﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FL.DBModule;
using FL.Lite;
using FL.SysEntity;

namespace FL.CodeGenerator
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string html =  Generator.GeneratorUI(this.txtTableName.Text,this.txtStorePath.Text);
            FL.Common.FileHelper.GenerateFile(this.txtStorePath.Text + this.txtTableName.Text, html.ToString(), ".aspx");
            this.rtPageCode.Text = html;
            html = Generator.GeneratorCode(this.txtTableName.Text, this.txtStorePath.Text, this.txtNamespace.Text);
            FL.Common.FileHelper.GenerateFile(this.txtStorePath.Text + this.txtTableName.Text,html.ToString(), ".cs");
            this.rtCSCode.Text = html;
        }


        private void GeneratorCode(string tableName)
        {
            this.txtTableName.Text = tableName;
            string html = Generator.GeneratorUI(this.txtTableName.Text, this.txtStorePath.Text);
            FL.Common.FileHelper.GenerateFile(this.txtStorePath.Text +"\\html\\"+ this.txtTableName.Text, html.ToString(), ".html");
            this.rtPageCode.Text = html;
            html = Generator.GeneratorCode(this.txtTableName.Text, this.txtStorePath.Text, this.txtNamespace.Text);
            FL.Common.FileHelper.GenerateFile(this.txtStorePath.Text +"\\model\\"+ this.txtTableName.Text, html.ToString(), ".cs");
            this.rtCSCode.Text = html;
        }
       
        
        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var list = Generator.getTables();
            CreateTree(list);
        }

        private void CreateTree(IList<v_sys_objects> list)
        {
            CreateTree("表", "U", list);
            CreateTree("视图", "V", list);
            CreateTree("存储过程", "P", list);
            CreateTree("函数", "FN", list);
        }

        private void CreateTree(string p_name,string x_type, IList<v_sys_objects> list)
        {
            TreeNode node = new TreeNode();
            node.Text = p_name;
            node.ToolTipText = p_name;
            IList<v_sys_objects> _tlist = list.Where(p =>  p.parent_id.TrimEnd() == x_type).ToList();
            if (_tlist != null)
            {
                foreach (var entity in _tlist)
                {
                    TreeNode _node = new TreeNode();
                    _node.Text = entity.name;
                    _node.ToolTipText = entity.name;
                    node.Nodes.Add(_node);
                }
            }
            treeView1.Nodes.Add(node);


        }

        private void treeView1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            FL.Bussiness.BaseBussnies bll = new Bussiness.BaseBussnies();
            bll.RunSqlCommand(this.richTextBox1.Text);
            MessageBox.Show("执行成功");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var node = this.treeView1.SelectedNode;
            this.txtTableName.Text = node.Text;
            if (!node.Text.ToString().StartsWith("U"))
            {
                FL.Bussiness.BaseBussnies bll = new Bussiness.BaseBussnies();
                DataTable dt = bll.GetDataBySqlComm("sp_helptext " + node.Text);
                if (dt == null) { return; }
                StringBuilder sb = new StringBuilder("");
                foreach (DataRow row in dt.Rows)
                {
                    sb.AppendLine(row.ItemArray[0].ToString());
                }
                this.richTextBox1.Text = sb.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var list = Generator.getTables();
            foreach(var entity in list)
            {
                if ((entity.xtype.Trim() == "U" || entity.xtype.Trim() == "V") && string.IsNullOrEmpty(entity.id))
                {
                    GeneratorCode(entity.name);
                }
            }
        }
    }
}
